package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.Coupons;
import com.ruoyi.system.mapper.CouponsMapper;
import com.ruoyi.system.service.ICouponsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 优惠券Service业务层处理
 *
 * @author ruoyi
 * @date 2024-03-22
 */
@Service
public class CouponsServiceImpl implements ICouponsService {
    @Autowired
    private CouponsMapper couponsMapper;
    
    /**
     * 查询优惠券
     *
     * @param id 优惠券主键
     * @return 优惠券
     */
    @Override
    public Coupons selectCouponsById(Long id) {
        return couponsMapper.selectCouponsById(id);
    }
    
    /**
     * 查询优惠券列表
     *
     * @param coupons 优惠券
     * @return 优惠券
     */
    @Override
    public List<Coupons> selectCouponsList(Coupons coupons) {
        List<Coupons> couponsList = couponsMapper.selectCouponsList(coupons);
        couponsList.stream().forEach(t -> {
            t.setStatusDesc(getStatusDesc(t.getStatus().intValue()));
            t.setTypeDesc(getTypeDesc(t.getType().intValue()));
            
        });
        return couponsList;
    }
    
    //-1-废弃 0-过期 1-正常
    private String getStatusDesc(Integer status) {
        switch (status) {
            case -1:
                return "废弃";
            case 0:
                return "过期";
            case 1:
                return "正常";
            default:
                return "未知";
        }
    }
    //101-普通满减 102-循环满减券 103-满折券 104-免单
    private String getTypeDesc(Integer type) {
        switch (type) {
            case 101:
                return "普通满减";
            case 102:
                return "循环满减券";
            case 103:
                return "满折券";
            case 104:
                return "免单";
            default:
                return "未知";
        }
    }
    
    /**
     * 新增优惠券
     *
     * @param coupons 优惠券
     * @return 结果
     */
    @Override
    public int insertCoupons(Coupons coupons) {
        coupons.setCreateTime(DateUtils.getNowDate());
        return couponsMapper.insertCoupons(coupons);
    }
    
    /**
     * 修改优惠券
     *
     * @param coupons 优惠券
     * @return 结果
     */
    @Override
    public int updateCoupons(Coupons coupons) {
        coupons.setUpdateTime(DateUtils.getNowDate());
        return couponsMapper.updateCoupons(coupons);
    }
    
    /**
     * 批量删除优惠券
     *
     * @param ids 需要删除的优惠券主键
     * @return 结果
     */
    @Override
    public int deleteCouponsByIds(String ids) {
        return couponsMapper.deleteCouponsByIds(Convert.toStrArray(ids));
    }
    
    /**
     * 删除优惠券信息
     *
     * @param id 优惠券主键
     * @return 结果
     */
    @Override
    public int deleteCouponsById(Long id) {
        return couponsMapper.deleteCouponsById(id);
    }
}
